package cn.edu.hhu.servlet.student;

import cn.edu.hhu.Bean.AttendanceInfo;
import cn.edu.hhu.Bean.AttendanceTask;
import cn.edu.hhu.Bean.Student;
import cn.edu.hhu.DAO.AttendanceInfoDao;
import cn.edu.hhu.DAO.impl.AttendanceInfoDaoImpl;
import cn.edu.hhu.DAO.impl.AttendanceTaskDaoImpl;
import cn.edu.hhu.service.Impl.StudentServiceImpl;
import cn.edu.hhu.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;

/**
 * @anthor: lddd
 * @date: 2020/5/11 - 10:39
 * @description:
 **/
@WebServlet("/student/signinServlet")
public class SigninServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String task_num = request.getParameter("task_num");//签到任务id
        request.getSession().setAttribute("task_num",task_num);
        Student loginStudent = (Student) request.getSession().getAttribute("loginStudent");//登录的学生用户
        //查询签到信息
        StudentService studentService = new StudentServiceImpl();
        AttendanceTask submit_Task = new AttendanceTask();
        submit_Task.setTnum(Long.parseLong(task_num));
        AttendanceInfo sql_Info = studentService.queryAttendanceInfo(loginStudent, submit_Task);
        //查询当前任务
        AttendanceTaskDaoImpl TaskDao = new AttendanceTaskDaoImpl();
        AttendanceTask sql_Task = TaskDao.queryTaskByTnum(submit_Task.getTnum());
        System.out.println(sql_Task);
        request.setAttribute("attendanceTask", sql_Task);
        if (sql_Info != null) {
            //有签到记录,说明已经签过到
            request.setAttribute("attendanceInfo", sql_Info);
            request.getRequestDispatcher("/student/signinResult/success.jsp").forward(request,response);
            System.out.println("签到成功！");
        } else {
            Timestamp now = new Timestamp(System.currentTimeMillis());
            Timestamp start_time = sql_Task.getStart_time();
            Timestamp end_time = sql_Task.getEnd_time();
            System.out.println(now);
            System.out.println(start_time);
            if (now.compareTo(start_time) < 0) {
                //活动未开始
                request.getRequestDispatcher("/student/signinResult/notStart.jsp").forward(request,response);
                System.out.println("活动未开始！");
            } else if (now.compareTo(end_time) > 0) {
                //活动已结束
                request.getRequestDispatcher("/student/signinResult/over.jsp").forward(request,response);
                System.out.println("活动已结束！");
            } else {
                //可以签到
                System.out.println("欢迎签到！");
                request.setAttribute("task_num", task_num);
                request.setAttribute("end_time", sql_Task.getEnd_time());
                request.getRequestDispatcher("/student/signin.jsp").forward(request, response);
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
